package spitfire.ksim.view;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.BoundedRangeModel;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.NumberFormatter;

/**
 * KTunerPanel is a composed GUI component made up of 
 * a caption, a slide bar, and a text field.
 * It is designed for a tunable property, whose bounds, 
 * value and caption are provided by a KTunerModel.
 * </br></br>
 * The slide bar and text field both can modify and 
 * indicate the current value of the property. And they 
 * are changed simultaneously if either is changed. They 
 * are both constrained with integer type and with in 
 * the bounds of the model.
 * 
 * @author Adam
 */
public class KTunerPanel extends JPanel {
	
	private static final long serialVersionUID = -3353149717114344508L;
	private JLabel caption;
	private JSlider slider;
	private JFormattedTextField text;
	private JLabel unit;
	private BoundedRangeModel model;
	
	public KTunerPanel(String captionStr, String unitStr, BoundedRangeModel myModel) {
		model = myModel;
		model.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				text.setValue(model.getValue());
			}
		});
		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		
		// caption
		caption = new JLabel(captionStr); 
		caption.setPreferredSize(new Dimension(80, 20));
		caption.setMinimumSize(new Dimension(80, 20));
		caption.setMaximumSize(new Dimension(80, 20));
		caption.setAlignmentX(CENTER_ALIGNMENT);
		add(caption);
		
		// slider
		slider = new JSlider(model);
		slider.setPreferredSize(new Dimension(180, 28));
		slider.setMinimumSize(new Dimension(100, 28));
		slider.setMaximumSize(new Dimension(350, 28));
		slider.setMajorTickSpacing((model.getMaximum() - model.getMinimum()) / 2);
		slider.setMinorTickSpacing((model.getMaximum() - model.getMinimum()) / 10);
		slider.setPaintTicks(true);
		add(slider);
		
		// text field
		NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(0);
        NumberFormatter formatter = new NumberFormatter(numberFormat);
        formatter.setMinimum(model.getMinimum());
        formatter.setMaximum(model.getMaximum());
        formatter.setAllowsInvalid(false);
        formatter.setCommitsOnValidEdit(true);
		text = new JFormattedTextField(formatter);
		text.setColumns(6);
		text.setValue(model.getValue());
		text.setPreferredSize(new Dimension(70, 20));
		text.setMinimumSize(new Dimension(60, 20));
		text.setMaximumSize(new Dimension(70, 20));
		text.addPropertyChangeListener(new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				if ("value".equals(evt.getPropertyName())) {
					Number value = (Number)evt.getNewValue();
					model.setValue(value.intValue());
					model.setValueIsAdjusting(true);
					model.setValueIsAdjusting(false);
				}
			}
		});
		add(text);
		
		// unit
		unit = new JLabel(unitStr);
		unit.setPreferredSize(new Dimension(20,20));
		unit.setMinimumSize(new Dimension(20,20));
		unit.setMaximumSize(new Dimension(20,20));
		add(unit);
	}
}
